# -*- coding: utf-8 -*-
import serial,sys,time,json,redis,binascii,math,traceback
flog=open("/home/wwwroot/pavgps/temperature.log",'w')
portType=0

try:
    rd = redis.Redis(host='127.0.0.1',port=6379)
    conffile = open('/home/wwwroot/pavgps/config.json','r')
    confstr = conffile.read()
    conf = json.loads(confstr)
    ser = serial.Serial(conf["TempSerialPort"], 9600, timeout=0.5)
    if 'TempPortType'  in conf:
        portType = conf["TempPortType"]


except Exception as e:
    print("============")
    errstr=traceback.print_exc()
    flog.write(""+repr(e));
    flog.close()
    quit()


def getTemp(ser):
    temp = 0
    if ser!=None:
        ser.write("?T\r\n".encode())
        tmp = ser.readline().decode()
        print(tmp)

        if tmp.find('T') >0:
            temp = float(tmp[2:])
    return temp

def getTemp1(ser):
    temp = 0
    try:
        if ser!=None:
            cmd = bytes.fromhex("54 50 01 F1 96")
            ser.write(cmd)
            n=ser.inWaiting()                             #串口接收
            if n:
                data= str(binascii.b2a_hex(ser.read(n)))[2:-1]          #Hex转换成字符串
                hasstr =data.find('545001')
                if hasstr>-1:
                    fdata =data[hasstr:14]
                    temp= (int(fdata[8:10],16)*256+ int(fdata[10:12],16))/10
    except:
        temp = 0
    print(temp)
    return temp

def getTemp2(ser):
    temp = 0
    try:
        if ser!=None:
            cmd = bytes.fromhex("01 03 00 00 00 01 84 0A")
            ser.write(cmd)
            n=ser.inWaiting()                             #串口接收
            if n:
                data= str(binascii.b2a_hex(ser.read(n)))[2:-1]          #Hex转换成字符串
                hasstr =data.find('010302')
                if hasstr>-1:
                    fdata =data[hasstr:14]
                    temp = int(fdata[6:10],16)/10
    except:
        temp =  0
    print(temp)
    return temp

def getTemp3(ser):
    temp = 0
    cmd = bytes.fromhex("01 03 00 00 00 02 C4 0B")
    ser.write(cmd)
    n=ser.inWaiting()                             #串口接收
    if n:
        data= str(binascii.b2a_hex(ser.read(n)))[2:-1]
        hasstr =data.find('010304')
        if hasstr>-1:
            fdata =data[hasstr+6:14]
            temp=round(transfer(fdata),1)
    return temp

def transfer(hexStr):
	ret = str();
	for x in range(0,len(hexStr),2):
		a = hexStr[x:x+2];
		intItem = int(a,16);
		binnaryStr = bin(intItem)[2:len(bin(intItem))];
		binnaryStr = "%(binnary)08d"%{'binnary':int(binnaryStr)}
		ret = ret+binnaryStr;
	s= int(ret[0]);
	n = int(ret[1:9],2);
	mStr = ret[9:len(ret)-1];
	m = float();
	for x in range(1,len(mStr)-1,1):
		if mStr[x-1] == "1":
			m=m+math.pow(0.5,x);

	val = math.pow(-1,s)*(math.pow(2,n-127))*(1+m);
	return val;


while(1):
    tmp=0
    if portType=='0':
	    tmp = getTemp(ser)
    if portType=='1':
        tmp = getTemp1(ser)
    if portType=='2':
        tmp = getTemp2(ser)
    if portType=='3':
        tmp = getTemp3(ser)
    print(portType)
    print(tmp)
    if tmp>0:
        rd.set("temperature",tmp,10)
    time.sleep(1)
